#pragma once

#include <list>
#include <set>
#include "INI1.26.h"
using namespace std;

class ConfigFile
{
public:
	ConfigFile(const std::wstring& path, bool& result);
	ConfigFile(const std::string& module_name = "");
	~ConfigFile(void);

public:
	bool Parse(const std::wstring& file_path);
	void Save(const std::string& path = "");
	bool IsTodayUpdated();
	bool IsTodayReported();
	bool IsTodayRepaired();

	//USER SECTION
	inline DWORD GetBossKey() { return atoi(ini_["USER"]["bosskey"].c_str()); }
	inline void SetBossKey(DWORD value) { SetValue("USER", "bosskey", value); }
	inline BOOL IsBossKeyEnable() { return atoi(ini_["USER"]["bosskey_enable"].c_str()); }
	inline void SetBossKeyEnable(DWORD value) { SetValue("USER", "bosskey_enable", value); }

	inline std::string GetVersion() { return ini_["APP"]["ver"]; }
	inline void SetVersion(const std::string& value) { SetValue("APP", "ver", value); }
	inline std::string GetPassword() { return ini_["USER"]["password"]; }
	inline void SetPassword(const std::string& value) { SetValue("USER", "password", value); }
	inline bool IsNeedCheckUpdate() { return ini_["USER"]["UpdateDate"]!=GetTodayDate(); };
	inline void SetCheckUpdateDate(const std::string& value = GetTodayDate()) { SetValue("USER", "UpdateDate", value); }
	inline std::string GetNewVersion() { return ini_["USER"]["newer"]; }
	inline void SetNewVersion(const std::string& value) { SetValue("USER", "newer", value); }
	void SetWebLoginInfo(const std::string& value);// { SetValue("USER", "weblogin", value); }
	std::string GetWebLoginInfo();
	// runtime setting
	inline DWORD GetWindowSize() { return atoi(ini_["RUNTIME"]["winsize"].c_str()); }
	inline void SetWindowSize(DWORD value) { SetValue("RUNTIME", "winsize", value); }
	inline BOOL IsCloseAsHide() { return atoi(ini_["RUNTIME"]["closeashide"].c_str()); }
	inline void SetCloseAsHide(BOOL value) { SetValue("RUNTIME", "closeashide", value); }
	inline BOOL IsNeverAskClose() { return atoi(ini_["RUNTIME"]["neveraskclose"].c_str()); }
	inline void SetNeverAskClose(BOOL value) { SetValue("RUNTIME", "neveraskclose", value); }
	inline BOOL IsNeverAskBossKey() { return atoi(ini_["RUNTIME"]["neveraskbosskey"].c_str()); }
	inline void SetNeverAskBossKey(BOOL value) { SetValue("RUNTIME", "neveraskbosskey", value); }
	// timer
	int GetTimerType(int index);
	time_t GetTimerValue(int index);
	void SetTimerType(int index, int value);
	void SetTimerValue(int index, time_t value);
	inline std::string get_user_active() { return ini_["USER"]["Active"]; }
	inline void set_user_active(const std::string& value) { SetValue("USER", "Active", value); }		
	inline std::string get_user_first() { return ini_["USER"]["First"]; }
	inline void set_user_first(const std::string& value) { SetValue("USER", "First", value); }
	inline std::string get_user_finish() { return ini_["USER"]["Finish"]; }
	inline void set_user_finish(const std::string& value) { SetValue("USER", "Finish", value); }
	inline std::string get_user_postmsg() { return ini_["USER"]["PostMsg"]; };
	inline void set_user_postmsg(const std::string& value) { SetValue("USER", "PostMsg", value); }
	inline std::string get_user_packagetype() { return ini_["USER"]["Package"]; };
	inline void set_user_packagetype(const std::string& value) { SetValue("USER", "Package", value); }

	inline bool is_success() { return success_; }

	static std::string GetTodayDate();
private:
	//void ParseToList(const std::string& text, std::set<std::string>& result);
	static std::string GetTime();
	inline void SetValue(const std::string& section_name, const std::string& name, int value)
	{
		char str[20];
		itoa(value, str, 10);
		SetValue(section_name, name, str);
	}
	inline void SetValue(const std::string& section_name, const std::string& name, const std::string& value)
	{
		if (ini_[section_name][name].compare(value) != 0)
		{
			ini_[section_name][name] = value;
			m_mapItemsModified[section_name].insert(name);
			dirty_ = true;
		}
	}
	//void GetLocallowDirPath(std::wstring& result);
	//void GetLocalSettingDirPath(std::wstring& result);

private:
	INI<std::string, std::string, std::string> ini_;
	bool dirty_, success_;
	std::string	browser_;
	std::map<std::string, std::set<std::string> > m_mapItemsModified;
};

